package com.weasel.weixin.servlet;

import com.weasel.helper.JsonHelper;
import com.weasel.helper.LangHelper;
import com.weasel.helper.StringHelper;
import com.weasel.weixin.commons.CommonResponse;
import com.weasel.weixin.commons.accounts.Oauth2AccessTokenResponse;
import com.weasel.weixin.commons.user.UserInfoListResponse;
import com.weasel.weixin.commons.user.UserInfoRequest;
import com.weasel.weixin.commons.user.UserInfoResponse;
import com.weasel.weixin.mp.builder.MpConfigBuilder;
import com.weasel.weixin.mp.config.MpConfig;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * Created by Dylan on 2015/9/28.
 */
public class UserInfoServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);

        MpConfig config = MpConfigBuilder.fromXML("classpath:weixin-config.xml");

        String openIds = request.getParameter("openIds");
        String openId = request.getParameter("openId");
        String remark = request.getParameter("remark");

        UserInfoRequest userInfoRequest = new UserInfoRequest(config);

        if(StringUtils.isNotBlank(openIds)){
            String [] _openIds = StringUtils.split(openIds,",");
            try {
                if(_openIds.length == 1){
                    UserInfoResponse userInfoResponse = userInfoRequest.getUserInfo(_openIds[0]);
                    response.getWriter().write(JsonHelper.toJsonString(userInfoResponse));
                }
                if(_openIds.length > 1){
                    UserInfoListResponse userInfoListResponse = userInfoRequest.getUserInfoList(_openIds);
                    response.getWriter().write(JsonHelper.toJsonString(userInfoListResponse));
                }
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return;
        }

        if(StringUtils.isNotBlank(remark)){
            try {
                CommonResponse commonResponse = userInfoRequest.updateRemark(openId,remark);
                response.getWriter().write(JsonHelper.toJsonString(commonResponse));
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        response.getWriter().println("<html>");
        response.getWriter().println("<head>");
        response.getWriter().println("<meta charset=\"utf-8\">");
        response.getWriter().println("</head>");
        response.getWriter().println("<body>");
        response.getWriter().println("<form action='/userinfo' method='post'>");
        response.getWriter().println("<h1>opeinid(如果批量获取，请用','号隔开):<input name='openIds' type='text'/><input value='获取' type='submit'/></h1>");
        response.getWriter().println("</form>");
        response.getWriter().println("<form action='/userinfo' method='post'>");
        response.getWriter().println("<h1>opeinid:<input name='openId' type='text'/>remark:<input name='remark' type='text'/><input value='更新' type='submit'/></h1>");
        response.getWriter().println("</form>");
        response.getWriter().println("</body>");
        response.getWriter().println("</html>");
        response.getWriter().flush();
        response.getWriter().close();


    }
}
